package com.junbao.message.ui.web;

import java.io.IOException;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import net.sf.json.JSONObject;

import com.junbao.commom.context.ClassPathXmlApplicationContext;
import com.junbao.commom.exception.JunBaoException;
import com.junbao.commom.util.JsonUtils;
import com.junbao.message.application.service.MessageService;
import com.junbao.message.domain.enums.ProducerName;
import com.junbao.message.domain.model.NotifyMessage;
import com.junbao.message.infrastructure.core.RequestDto;
import com.junbao.message.infrastructure.core.dispatcher.MessageConfirmDisruptor;
import com.junbao.message.infrastructure.core.dispatcher.MessageConfirmSendDisruptor;
import com.junbao.message.infrastructure.core.dispatcher.MessageReserveDisruptor;
import com.junbao.message.infrastructure.core.dispatcher.MessageSendDisruptor;
import com.junbao.message.infrastructure.core.producer.MessageConfirmSendProducer;
import com.junbao.message.infrastructure.core.producer.MessageReserveProducer;
import com.junbao.message.infrastructure.core.producer.MessageSendProducer;
import com.junbao.message.infrastructure.result.Result;
import com.junbao.message.ui.dto.ReserveMessage;

/**
 * 消息确认发送
 * 对预存储消息确认发送
 * @author yilin
 *
 */
@SuppressWarnings("all")
@WebServlet("/confirm/send")
public class MessageConfirmSendServlet extends BaseServlet{

	private static final long serialVersionUID = 1L;

	private static final Logger LOGGER = LoggerFactory.getLogger(MessageConfirmSendServlet.class);
	
	@Override
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		try {
			String requestData = super.getRequestData(request, response);
			requestParmCheck(requestData, "message_squece");
			JSONObject source = JSONObject.fromObject(requestData);
			//检查消息是否存在
			MessageService messageService = (MessageService) ClassPathXmlApplicationContext.getInstance().getBean("messageService");
			NotifyMessage message = messageService.queryMessageByMessageSquece(source.getString("message_squece"));
			if (message == null) {
				LOGGER.info("根据序列号："+source.getString("message_squece")+"查询消息信息失败。");
				writer(response, Result.QUERY_MESSAGE_FAIL_BY_MESSAGESQUECE);
				return;
			}
			MessageConfirmSendDisruptor.instance()
			.getMessageProducer(ProducerName.MESSAGE_CONFIRM_SEND_PRODUCER)
			.sendData(new RequestDto<NotifyMessage>(message));
			writer(response, new Result(Result.SUCCESS.getResult_code(), Result.SUCCESS.getResult_msg()));
			return;
		} catch (JunBaoException e) {
			Result resultData = new Result(e.getErrCode(), e.getErrMsg());
			writer(response, resultData);
			return;
		} catch (Exception e) {
			Result resultData = new Result("9999", e.getMessage());
			writer(response, resultData);
			return;
		}
	}
	
}
